package com.linkface.base;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.annotation.NonNull;

import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoImpl;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.InvokeParam;
import com.jph.takephoto.model.TContextWrap;
import com.jph.takephoto.model.TResult;
import com.jph.takephoto.model.TakePhotoOptions;
import com.jph.takephoto.permission.InvokeListener;
import com.jph.takephoto.permission.PermissionManager;
import com.jph.takephoto.permission.TakePhotoInvocationHandler;
import com.linkface.utils.LFLog;

import java.io.File;

/**
 * Copyright (c) 2017-2018 LINKFACE Corporation. All rights reserved
 */
public class LFBaseTakePhotoActivity extends Activity implements InvokeListener, TakePhoto.TakeResultListener {
    private static final String TAG = "LFBaseTakePhotoActivity";

    private TakePhoto mTakePhoto;

    private InvokeParam mInvokeParam;

    protected Uri createTmpUri() {
        String imageLocation = Environment.getExternalStorageDirectory() + "/tmp.jpg";  // tmp file

        File f = new File(imageLocation);
        if(f.exists()) {
            f.delete();
        }

        return Uri.fromFile(f);
    }

    protected CropOptions getCropOptions(int width, int height, int aspectX, int aspectY) {
        CropOptions.Builder builder = new CropOptions.Builder();
        builder.setAspectX(aspectX).setAspectY(aspectY).setWithOwnCrop(true);
        builder.setOutputX(width).setOutputY(height);
        return builder.create();
    }

    protected TakePhoto getTakePhoto() {
        if(mTakePhoto == null) {
            mTakePhoto = (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this, this));
        }
        TakePhotoOptions.Builder builder = new TakePhotoOptions.Builder();
        builder.setCorrectImage(true);
        mTakePhoto.setTakePhotoOptions(builder.create());
//        CompressConfig config = new CompressConfig.Builder()
//                .enablePixelCompress(false)
//                .enableQualityCompress(false)
//                .create();
        mTakePhoto.onEnableCompress(null, true);  // 不压缩了 否则时间太长 等剪裁完以后再压缩
        return mTakePhoto;
    }

    @Override
    public void takeSuccess(TResult result) {
        LFLog.i(TAG, "takeSuccess");
    }

    @Override
    public void takeFail(TResult result, String msg) {
        LFLog.i(TAG, "takeFail");
    }

    @Override
    public void takeCancel() {
        LFLog.i(TAG, "takeCancel");
    }

    @Override
    public PermissionManager.TPermissionType invoke(InvokeParam invokeParam) {
        PermissionManager.TPermissionType type = PermissionManager.checkPermission(TContextWrap.of(this), invokeParam.getMethod());
        if(PermissionManager.TPermissionType.WAIT.equals(type)) {
            this.mInvokeParam = invokeParam;
        }
        return type;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.TPermissionType type = PermissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.handlePermissionsResult(this, type, mInvokeParam, this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        getTakePhoto().onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }
}
